package com.zzptc.syl.traffic;

import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class BlankFragment extends Fragment {

    public BlankFragment() {
    }

    private GridView gridView;
    private VideoAdapter myAdapter;
    private List<Uri> mData = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.one, container, false);
//        VideoView video = (VideoView) view.findViewById(R.id.VV_1);
        gridView = view.findViewById(R.id.gridView);
        mData=new ArrayList<>();
        Uri uri = copyFile("画.mp4");
        mData.add(uri);
        mData.add(uri);
        mData.add(uri);
        mData.add(uri);
        mData.add(uri);
        mData.add(uri);
        mData.add(uri);
        myAdapter = new VideoAdapter(mData,getActivity());
        gridView.setAdapter(myAdapter);
        return view;

    }
    public Uri copyFile(String name) {
        try {
            File dir = getActivity().getFilesDir();
            File file = new File(dir, name);
            if (file.exists()) {
                Log.d("Test", "=========file exist=========");
                return Uri.fromFile(file);
            } else {
                file.createNewFile();
                OutputStream os = new FileOutputStream(file);
                InputStream is = getActivity().getAssets().open(name);
                byte[] buffer = new byte[1024];
                int bufferRead = 0;
                while((bufferRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bufferRead);
                }
                os.flush();
                is.close();
                os.close();
                Log.d("Test", "=========copyFile success=========");
                return Uri.fromFile(file);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }



}